package joc.internal.transformer;

import javassist.ClassPool;
import javassist.CtBehavior;
import javassist.CtClass;
import joc.PreConditionHook;

public class CustomPreHookBehaviorTransformer extends CustomHookBehaviorTransformer {
	public CustomPreHookBehaviorTransformer(ClassPool pool) {
		super(pool);
	}

	@Override
	public void transform(CtClass clazz, CtBehavior behavior) throws Exception {
		logger.info("behavior " + behavior.getLongName());
		if (behavior.hasAnnotation(PreConditionHook.class)) {
			PreConditionHook annotation = (PreConditionHook) behavior.getAnnotation(PreConditionHook.class);
			handleHooks(behavior, annotation);
		}
		else if (behavior.hasAnnotation(PreConditionHook.List.class)) {
			PreConditionHook.List listAnnotation = (PreConditionHook.List) behavior.getAnnotation(PreConditionHook.List.class);
			for (PreConditionHook annotation : listAnnotation.value()) {
				handleHooks(behavior, annotation);
			}
		}
	}

	protected void handleHooks(CtBehavior behavior, PreConditionHook annotation) throws Exception {
		for (Class<?> hookClass : annotation.value()) {
			applyHook(behavior, hookClass.getName(), annotation.id());
		}
	}
}
